package com.zzy.demo.common.util.export;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

import com.zzy.cms.core.util.file.ZipUtils;
import com.zzy.demo.common.constant.FileConsant;

/**
 * @author 24386
 */
public class ExportToUtil {
	
	public static void exportToFile(HttpServletResponse response, String newFilePath, String fileName) throws IOException {
    	response.reset();
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") );
        response.setContentType("application/octet-stream; charset=utf-8");
        
        File file = new File(newFilePath);
        InputStream inputStream = new FileInputStream(file);
        try {
			IOUtils.copy(inputStream, response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
	        inputStream.close();
		}
	}
	
	
	
	
    /**
     * 响应流信息
     * @param response
     * @param zipOutPath
     * @param fileName
     * @return void
     * @author jiangrushe
     * @date 下午4:52 2021/9/3
     **/
	public static void responseBodyZip(HttpServletResponse response, String zipOutPath, String fileName) throws IOException {
    	response.reset();
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + FileConsant.ZIP_SUFFIX, "UTF-8"));
        response.setContentType("application/octet-stream; charset=utf-8");
        ZipUtils.compress(zipOutPath, response.getOutputStream(), fileName);
    }

}
